package com.spring.security.web.controller;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.spring.security.web.model.BaseResult;
public class BaseController {
	public   Log LOGGER = LogFactory.getLog(getClass());
    SerializerFeature[] feature =
            { SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse,
                    SerializerFeature.WriteMapNullValue };

    /**
     * @description: 构造成功返回结果
     * @param resultData  : 需要返回的数据，可选
     * @return
     */
    protected Object buildSuccessResultInfo(Object resultData,String successMsg)
    {
    	if(StringUtils.isBlank(successMsg)){
    		successMsg = "操作成功";
    	}
//        LOGGER.debug(String.format("enter function, %s", resultData));
        BaseResult resultVo = new BaseResult();
        resultVo.setDataset(resultData);
        resultVo.setMsg(successMsg);
//        resultVo.setStatus(HttpStatus.OK);
        resultVo.setStatus("1");
        return  resultVo;
      //  return JSON.toJSONString(resultVo, feature);
    }
    
    protected Object buildSuccessResultInfo(Object resultData,String successMsg,String callback){
    	if(StringUtils.isNotBlank(callback)){
    		return new JSONPObject(callback, buildSuccessResultInfo(resultData,successMsg));
    	}else{
    		return buildSuccessResultInfo(resultData,successMsg);
    	}
    }

    /**
     * @description: 构造失败返回结果
     * @param resultCode
     *            :任意非0数字，代表失败
     * @param failedMsg
     *            ：失败信息
     * @return
     */

    protected Object buildFailedResultInfo(String resultCode, String failedMsg)
    {
        BaseResult resultVo = new BaseResult(resultCode, failedMsg);
       // return JSON.toJSONString(resultVo, feature);
        return resultVo;
    }
    
    protected Object buildFailedResultInfo(String resultCode, String failedMsg,String callback){
    	if(StringUtils.isBlank(resultCode)){
    		resultCode = "0";
    	}
    	if(StringUtils.isBlank(failedMsg)){
    		failedMsg = "操作失败";
    	}
    	if(StringUtils.isNotBlank(callback)){
    		return new JSONPObject(callback,buildFailedResultInfo(resultCode,failedMsg));
    	}else{
    		return buildFailedResultInfo(resultCode,failedMsg);
    	}
    }
    
    
    /**
     * @description: 构造成功返回结果
     * @param resultData  : 需要返回的数据，可选
     * @return
     */
    protected Object buildResultInfo(BaseResult resultVo)
    {
    	if(StringUtils.isBlank(resultVo.getMsg())){
    		resultVo.setMsg("操作成功");
    	}
    	if(StringUtils.isBlank(resultVo.getStatus())){
    		resultVo.setStatus("1");
    	}
        return  resultVo;
    }
    
    protected Object buildResultInfo(BaseResult resultVo,String callback){
    	if(StringUtils.isNotBlank(callback)){
    		return new JSONPObject(callback, buildResultInfo(resultVo));
    	}else{
    		return buildResultInfo(resultVo);
    	}
    }
    
    
    /**
     * @description: 构造成功返回结果
     * @param resultData  : 需要返回的数据，可选
     * @return
     */
    protected Object buildResultObject(Object obj)
    {
        return  obj;
    }
    
    protected Object buildResultObject(Object obj,String callback){
    	if(StringUtils.isNotBlank(callback)){
    		return new JSONPObject(callback, buildResultObject(obj));
    	}else{
    		return buildResultObject(obj);
    	}
    }
}
